package cn.wangxl.vertx.starter.web;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;

/**
 * 样例：
 * 单处理器路由的vert.x web
 * 指定http方法
 */
public class RouterOneVerticle extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {
    Vertx vertx = Vertx.vertx();
    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(vertx);

    Route route = router.route("/some/path").method(HttpMethod.GET);

    route.handler(routingContext -> {

      HttpServerResponse response = routingContext.response();
      System.out.println("执行route");

      // response.write("route");// 报错，请在response().end(结果)---返回结果

      // Now end the response
      routingContext.response().end("route");
    });
    server.requestHandler(router).listen(8888, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
  }

  private Future<Void> startHttpServer() {
    Future<Void> future = Future.future();
    // (...)
    return future;
  }


  public static void main(String[] args) throws Exception {
    new RouterOneVerticle().start(Future.future());
  }
}
